package lv.dysnomia.server.entities

import java.util.{List=>JavaList}
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import lv.dysnomia.portal.web.PMF;

object MediaFileDAO {
  def removeByKey(key: String): Unit = {
    val pm = PMF.getInstance.getPersistenceManager
    try {
      val query = pm.newQuery((new MediaFile).getClass)
      query.setFilter("blobKey == blobKeyParam")
      query.declareParameters("String blobKeyParam")
      query.deletePersistentAll(key)
    } finally {
      pm.close()
    }
  }

  def getKeyByFname(fname: String): String = {
    val pm = PMF.getInstance.getPersistenceManager
    val result: String = try {
      val query: Query = pm.newQuery((new MediaFile).getClass)
      query.setFilter("fileName == fileNameParam")
      query.declareParameters("String fileNameParam")
      val results: JavaList[MediaFile] = query.execute(fname).asInstanceOf[JavaList[MediaFile]]
      if (results != null && results.size() > 0) results.get(0).getBlobKey
      else ""
    } catch {
      case e: Exception => { println(e.toString); "" }
    }
    finally {
      pm.close();
    }
    return result
  }
}